#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
LL n,MOD;
struct Matrix
{
	LL num[4][4];
	Matrix operator* (const Matrix a)const
	{
		Matrix ret;
		memset(ret.num,0,sizeof(ret.num));
		for(LL i=1;i<=3;++i)
			for(LL j=1;j<=3;++j)
				for(LL k=1;k<=3;++k)
				{
					ret.num[i][j]=(ret.num[i][j]+num[i][k]*a.num[k][j]%MOD)%MOD;
				}
		return ret;
	}
};
Matrix qpow(Matrix a,LL b,Matrix ans)
{
	for(;b;b>>=1,a=a*a)
		if(b&1)ans=ans*a;
	return ans;
}
int main()
{
	scanf("%lld%lld",&n,&MOD);
	Matrix a,b;
	LL k=10;
	memset(a.num,0,sizeof(a.num));
	memset(b.num,0,sizeof(b.num));
	a.num[1][1]=0;a.num[1][2]=0;a.num[1][3]=1;
	for(LL j=0;;j=j*10+9,k*=10)
	{
		b.num[2][1]=1;b.num[2][2]=1;b.num[3][1]=1;b.num[3][2]=1;b.num[3][3]=1;
		b.num[1][1]=k%MOD;
		if(n<=j*10+9)
		{
			a=qpow(b,n-j,a);
			printf("%lld\n",a.num[1][1]);
			return 0;
		}
		else a=qpow(b,j*10+9-j,a);
	}
	return 0;
}
